home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 Christmas / macformat-045.iso / Shareware Plus / Developers / enteract-377 / enteract-377.sit / EnterAct Stuff / write your own Drag_on / CodeResource_Files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-22  |  4.0 KB  |  3 lines  |  [TEXT/KEEN]

  1. CodeResource_Files.cTEXTKEEN
  2.  ò´˜Z´˜ZÅÅ=–/* CodeResource_Files.c - read stdin, write stdout
  3. using Mac calls rather than stdio. */
  4.  
  5. /* stdin and stdout are just ordinary text files, the full path
  6. names for these files being supplied by the calling application
  7. in gacc.stdInFileName and gacc.stdOutFileName - gacc is a global
  8. copy of the AppCodeComm struct passed to the code resource by the
  9. application (see Code_Main.c). The full path name is converted to
  10. a regular file name and vRefNum, and then things proceed in a very
  11. standard way, like reading or saving using SFGetFile or SFPutFile.
  12. */
  13.  
  14. #include "CodeResource.h"
  15. #include "AppCodeComm.h"
  16. #include <string.h>
  17.  
  18. Handle ReadStdIn(void);
  19. Boolean PutToStdOut(Handle hText);
  20. short GetNameAndVRefNum(char *name, char *filename);
  21.  
  22. /* Read in the contents of stdin to a handle. Note if there is
  23. nothing in the file then NULL is returned rather than an empty
  24. handle. The full path name of stdin is in gacc.stdInFileName. */
  25. Handle ReadStdIn()
  26.     {
  27.     Handle        hText = NULL;
  28.     long        count;
  29.     short            saveVol, refNum, vRefNum;
  30.     char        filename[32];
  31.     Boolean        opened = FALSE;
  32.     
  33.     if (GetVol(NULL, &saveVol))
  34.         saveVol = 0;
  35.     if ((vRefNum = GetNameAndVRefNum(gacc.stdInFileName, filename)) == 0)
  36.         goto JumpOut;
  37.     
  38.     if (FSOpen((StringPtr)filename, vRefNum, &refNum))
  39.         goto JumpOut;
  40.     else
  41.         opened = TRUE;
  42.     if (GetEOF(refNum, &count) || count <= 0L)
  43.         goto JumpOut;
  44.     hText = NewHandle(count);
  45.     if (MemError() != noErr)
  46.         {
  47.         MemoryAlert();
  48.         goto JumpOut;
  49.         }
  50.     if (FSRead(refNum, &count, *hText))
  51.         {
  52.         DisposHandle(hText);
  53.         hText = NULL;
  54.         goto JumpOut;
  55.         }
  56. JumpOut:
  57.     if (opened)
  58.         FSClose(refNum);
  59.     if (saveVol)
  60.         SetVol(NULL, saveVol);
  61.     return(hText);
  62.     }
  63.  
  64.  
  65. /* Write the text in hText to stdout as specified by the full path name
  66. in gacc.stdOutFileName. */
  67. Boolean PutToStdOut(Handle hText)
  68.     {
  69.     long        count;
  70.     short            saveVol, refNum, vRefNum;
  71.     char        filename[32];
  72.     
  73.     if (GetVol(NULL, &saveVol))
  74.         saveVol = 0;
  75.     if ((vRefNum = GetNameAndVRefNum(gacc.stdOutFileName, filename)) == 0)
  76.         goto JumpOut;
  77.     
  78.     FSDelete((StringPtr)filename, vRefNum);
  79.     if (Create((StringPtr)filename, vRefNum, '????', 'TEXT'))
  80.         goto JumpOut;
  81.     if (FSOpen((StringPtr)filename, vRefNum, &refNum))
  82.         goto JumpOut;
  83.     count = GetHandleSize(hText);
  84.     if (FSWrite(refNum, &count, *hText))
  85.         {
  86.         FSClose(refNum);
  87.         goto JumpOut;
  88.         }
  89.     FSClose(refNum);
  90.     
  91.     FlushVol(NULL, vRefNum);
  92.     if (saveVol)
  93.         SetVol(NULL, saveVol);
  94.     return(TRUE);
  95.     
  96. JumpOut:
  97.     if (saveVol)
  98.         SetVol(NULL, saveVol);
  99.     return(FALSE);
  100.     }
  101.  
  102. /* Convert the full path name "name" into filename and vRefNum.
  103. "name" looks like "DiskName:folder1:folder2:...folderN:filename",
  104. so we back up from the end of "name" to the first colon to determine
  105. the filename. */
  106. short GetNameAndVRefNum(char *name, char *filename)
  107.     {
  108.     WDPBRec        theParms;
  109.     short         len = strlen(name);
  110.     Ptr            endPtr = name + len, startPtr = endPtr;
  111.     char        volname[256];
  112.     
  113.     while (startPtr >= name)
  114.         {
  115.         if (*startPtr == ':')
  116.             break;
  117.         --startPtr;
  118.         }
  119.     ++startPtr;
  120.     if (startPtr >= endPtr)
  121.         return(0);
  122.     filename[0] = endPtr - startPtr;
  123.     BlockMove(startPtr, filename+1, filename[0]);
  124.     volname[0] = startPtr - name;
  125.     BlockMove(name, volname+1, startPtr - name);
  126.     theParms.ioVRefNum = 0;
  127.     theParms.ioNamePtr = (StringPtr)volname;
  128.     theParms.ioWDDirID = 0;
  129.     theParms.ioWDProcID = 'ERIK';
  130.     if (PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  131.         return(0);
  132.     return(theParms.ioVRefNum);
  133.     }
  134. **nveVol);
  135.     return(FALSE);
  136.     }
  137. ;
  138. intCodeResource_Files.csTEXTKETEXTKEENh§◊8⁄
  139.  òid);
  140. Boolean HasSetWatchCursor(void);÷ .ˇ¸∞Æˇ¯f .ˇÙ∞Æˇfp`“ .ˇ¸∞Æˇ¯f .ˇÙ∞Æˇgpˇ`∏ .ˇ¸∞Æˇ¯g .ˇÙ∞Æˇfp`û nˇ¸p=@ˇÓ n
  141.     Monaco´±
  142. 2,‡**n≠≈‘nETAB"EFNT.JAGZ:WSIZFÏˇˇ≠√0Ρˇ≠√Ìˇˇ≠√8Óˇˇ≠√4